var common = {
	localurl:'https://nbnet.chinaecdc.com',
	url:'https://nbnet.chinaecdc.com',
	csurl:'https://nbnet.chinaecdc.com',
	csurl2:'https://nbnet.chinaecdc.com',
	appid:'wx10c4928667763190',
	secret:'0b066ffbdd114feea1ae2bdae109b058',
	add0:function(m){
		return m<10?'0'+m:m 
	},
	format:function(shijianchuo){
		//shijianchuo是整数，否则要parseInt转换
		var time = new Date(shijianchuo);
		var y = time.getFullYear();
		var m = time.getMonth()+1;
		var d = time.getDate();
		return y+'-'+common.add0(m)+'-'+common.add0(d);
	},
	//授权登录
	bindGetUserInfo(e){
		var that = this;
		if(e.detail.userInfo){
			//用户点击了授权按钮
			//调取用户信息
			wx.getUserInfo({
				success(res){
					let encryptedData = res.encryptedData;
					let iv = res.iv;
					uni.setStorageSync('encryptedData',res.encryptedData)
					uni.setStorageSync('iv',res.iv);
					uni.setStorageSync('avatarUrl',res.userInfo.avatarUrl);
					uni.setStorageSync('nickName',res.userInfo.nickName)
					that.$store.commit('changeAvatarUrl',res.userInfo.avatarUrl)
					that.$store.commit('changeNickName',res.userInfo.nickName)
					//调取 wx。login。获取uniID
					wx.login({
						success(res){
							let code = res.code;
							uni.request({
								url:'https://api.weixin.qq.com/sns/jscode2session?appid='+common.appid+'&secret='+common.secret+'&js_code='+code+'&grant_type=authorization_code',
								success(res){
									//获取sessionKey和openId
									uni.setStorageSync('openid',res.data.openid);
									uni.setStorageSync('sessionKey',res.data.session_key);
									let sessionKey = res.data.session_key ;
									//获取uniId
									uni.request({
										url:common.url + '/wx/mini/aes.json?sessionKey='+encodeURIComponent(sessionKey)+'&encryptedData='+encodeURIComponent(encryptedData)+'&iv='+encodeURIComponent(iv),
										success(res){
											uni.setStorageSync('unionId',res.data.unionId);
											let unionId = res.data.unionId;
											uni.request({
												url:common.csurl2+'/app/user/judgePhone.json',
												data:{
													unionid:unionId
												},
												success(res){
													//如果没绑定手机，进入绑定手机页面
													if(!res.data.ret){
														uni.navigateTo({
															url: '/pages/phone/phone'
														});
													}else{
														console.log('绑定了')
														that.$store.commit('isloginfn',true);
														uni.setStorageSync('islogin',true);
													}
												}
											})
										}
									})
								}
							})
						}
					})
				}
			})	
		}
	},
	loginfn:function(asd) {
		var that = asd;
		uni.getProvider({
			service: 'oauth',
			success: function(res) {
				if (~res.provider.indexOf('weixin')) {
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							// 获取用户信息
							uni.getUserInfo({
								provider: 'weixin',
								success: function(infoRes) {
									that.uerInfo = infoRes.userInfo;
									uni.setStorage({
										key: 'uerInfo',
										data: that.uerInfo
									});		
									console.log(that.uerInfo)
									uni.request({
										url: common.localurl+'/app/user/login.json',
										data: {
											unionid: infoRes.userInfo.unionId
										},
										success: (res) => {
											//判定是否存在手机号
											uni.request({
												url:common.csurl2+'/app/user/judgePhone.json',
												data:{
													unionid:infoRes.userInfo.unionId
												},
												success:(res)=>{
													if(!res.data.ret){
														uni.navigateTo({
															url: '/pages/phone/phone'
														});
													}else{
														that.$store.commit('isloginfn',true);
														uni.setStorage({
															key: 'islogin',
															data: true
														});
													}
												}
											})
										}
									});
									
								},
								complete:(infoRes)=>{
									//console.log(JSON.stringify(infoRes))
								}
							});
						}
					});
				}
			}
		});
	},
	toupdata(that,islogin) {
		if(islogin){
			plus.nativeUI.showWaiting("检测更新...")
		}
		var version = ''
		plus.runtime.getProperty(plus.runtime.appid, function(inf) {
			version = inf.version;
			//首先判断版本
			var checkurl = common.localurl+'/api/edition/list.json';
			uni.request({
				url: checkurl,
				method: 'GET',
				success: (res) => {
					plus.nativeUI.closeWaiting();
					var newversion = res.data.data.editionInformation;
					var downloadurl = common.localurl+res.data.data.editionLink;
					if (version && newversion && (Number(version) < Number(newversion))) {
						if(islogin){
							plus.nativeUI.confirm("请确认是否更新版本？", function(e) {
								if (e.index == 1) {
									if(res.data.data.packagemethod ==1 ){    //==1为全部更新，其他情况为2为内更新
										downloadurl = common.localurl+'/assets/front/jyzx.apk';
										if( plus.os.name =="Android"){
											common.downWgt(downloadurl,that);
										}else{
											var url='itms-apps://itunes.apple.com/cn/app/%E6%95%99%E8%82%B2%E4%B9%8B%E5%BC%A6/id1317663412?from=singlemessage&isappinstalled=0';// HelloH5应用在appstore的地址  
											plus.runtime.openURL(url);  
										}
									}else{
										
										common.downWgt(downloadurl,that);
									}
								} else {
								}
							}, "有新版本需要更新", ["取消", "升级"]);
						}else{
							var oldres = res;
							uni.getStorage({
								key:'uerchoose',
								complete(res) {
									if(Number(res.data)<Number(newversion)){
										plus.nativeUI.confirm("请确认是否更新版本？", function(e) {
											if (e.index == 1) {
												if(oldres.data.data.packagemethod ==1 ){   //==1为全部更新，其他情况为2为内更新
													downloadurl = common.localurl+'/assets/front/jyzx.apk';
													if( plus.os.name =="Android"){
														common.downWgt(downloadurl,that);
													}else{
														var url='itms-apps://itunes.apple.com/cn/app/%E6%95%99%E8%82%B2%E4%B9%8B%E5%BC%A6/id1317663412?from=singlemessage&isappinstalled=0';// HelloH5应用在appstore的地址  
														plus.runtime.openURL(url);  
													}
												}else{
													common.downWgt(downloadurl,that);
												}
											} else {
												uni.setStorage({
													key: 'uerchoose',
													data:newversion
												});
											}
										}, "有新版本需要更新", ["取消", "升级"]);
									}
								}
							})
							
						}
					} else {
						if(islogin){
							plus.nativeUI.toast("无最新版本更新!");
						}
					}
				}
			});
		});
		
	},
	//下载
	downWgt(downloadurl,that) {
		plus.nativeUI.showWaiting("下载中...")
		uni.downloadFile({
			url: downloadurl, 
			success: (res) => {
				if (res.statusCode == 200) {
					plus.nativeUI.showWaiting("安装中...");
					common.installWgt(res.tempFilePath); // 安装wgt包
				} else {
					plus.nativeUI.toast("下载失败！");
				}
				plus.nativeUI.closeWaiting();
			},
			complete: (res) => {
				console.log(res.statusCode);
				console.log(res.tempFilePath);
			}
		});
	},
	//安装
	installWgt(path) {
		plus.nativeUI.showWaiting("安装中...");
		plus.runtime.install(path, {
			force: false
		}, function() {
			plus.nativeUI.closeWaiting();
			plus.runtime.restart();
		}, function(e) {
			plus.nativeUI.closeWaiting();
			plus.nativeUI.toast("安装中[" + e.code + "]：" + e.message);
		});
	}
}
export {
	common
}